Repository navigation
fix(sessions): preserve append order for tied timestamps in SqliteSessionService - #7287
Closed
chelsealong wants to merge 1 commit into
Closed
chelsealong wants to merge 1 commit into
chelsealong wants to merge 1 commit into
Conversation
…sionService SqliteSessionService.get_session broke timestamp ties with `ORDER BY id DESC`. Event ids are random, so tied events come back in an order unrelated to how they were appended, reshuffling a replayed conversation and, for a resumable Workflow, handing ReplayManager a completion order it can never reproduce. Break ties on `rowid` instead, which reflects actual insertion order. Fixes google#7286 for SqliteSessionService. DatabaseSessionService has the same class of bug but needs a portable insertion-order column across SQLite and Postgres, which is a larger schema/migration change left for a follow-up.
copybara-service Bot
pushed a commit
that referenced
this pull request
Oct 2, 2026
Merge #7287 Break timestamp ties on SQLite rowid instead of random event id in SqliteSessionService.get_session so events sharing a timestamp reload in the order they were appended. DatabaseSessionService remains a separate follow-up requiring a cross-dialect migration. Part of #7286 PiperOrigin-RevId: 992076400
Collaborator
|
Thank you @chelsealong for your contribution! 🎉 Your changes have been successfully imported and merged via Copybara in commit f33343f. Closing this PR as the changes are now in the main branch. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
SqliteSessionService; see Scope below)Problem:
SqliteSessionService.get_sessionreloads a session's events ordered byORDER BY timestamp DESC, id DESC. Event ids are random(
Event.new_id()), so when two events share a timestamp they come back inan order unrelated to how they were appended — merely "stable" (the same
order on every read), not the actual append order.
Equal timestamps are common: Python 3.12 on Windows ticks every 15.6 ms by
default, and fast function nodes emit several events per tick on any OS.
For a resumable graph
Workflow, this breaks resume:ReplayManagerbuilds the replay sequence barrier from the reloaded events, so a swapped
pair gives the barrier a completion order the workflow can never follow,
and resume fails after the 15s barrier timeout with
RuntimeError: Replay divergence detected: ....Solution:
Break timestamp ties on SQLite's implicit
rowidinstead ofid.rowidreflects actual insertion order for this table (it is a normalrowid table — no
WITHOUT ROWIDclause, and the declared primary key isnon-integer, so SQLite maintains the implicit monotonically increasing
rowid), so ordering on it preserves append order for tied timestamps.
Scope:
The issue also reports the same class of bug in
DatabaseSessionService(
order_by(timestamp.desc(), id.desc()), used both inget_sessionandin the markerless-session staleness check). Fixing that correctly needs a
portable insertion-order column across both SQLite and Postgres (there is
no equivalent of SQLite's
rowidavailable generically throughSQLAlchemy across dialects), which means a schema/migration change well
beyond a minimal fix. I'm leaving that as a follow-up rather than bundling
a larger, riskier schema change into this PR.
Testing Plan
Unit Tests:
Added
test_sqlite_session_service_preserves_append_order_on_tied_timestampsto
tests/unittests/sessions/test_session_service.py, asserting thatSqliteSessionService.get_sessionreturns tied-timestamp events in theorder they were appended (both forward and reverse append order).
Also split the pre-existing
test_get_session_orders_tied_timestamps_by_id(which asserted id-order for both
DatabaseSessionServiceandSqliteSessionService) so it now documents only the still-openDatabaseSessionServicebehavior;SqliteSessionServiceis covered by thenew append-order test instead.
pre-commit run --files src/google/adk/sessions/sqlite_session_service.py tests/unittests/sessions/test_session_service.pypasses (isort, pyink,ruff, addlicense, compliance checks, codespell). The one hook failure it
reports (
check-new-py-prefixonsrc/google/adk/memory/_sqlite_memory_service.py)is pre-existing on
main/unmodified HEAD and unrelated to this change.Checklist
AI assistance disclosure
This change (analysis, fix, and test) was developed with the assistance of
Claude Code (Anthropic).